package com.chengliang.bbs.controller;

import com.chengliang.bbs.entry.Jie;
import com.chengliang.bbs.entry.Reply;
import com.chengliang.bbs.service.JieServsice;
import com.chengliang.bbs.service.ReplyService;
import com.chengliang.bbs.type.ColumnType;
import com.chengliang.bbs.type.FilterType;
import com.chengliang.bbs.type.TechnologyType;
import com.chengliang.bbs.util.StringUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author chengliang
 * 首页
 */
@Slf4j
@Controller
public class IndexController {

    private final JieServsice jieServsice;

    private final ReplyService replyService;

    @Autowired
    public IndexController(JieServsice jieServsice, ReplyService replyService) {
        this.jieServsice = jieServsice;
        this.replyService=replyService;
    }

    @RequestMapping("/")
    public String index(Model model){
        Page<Jie> jieList = jieServsice.getJieList(null, null, 1, 20);
        model.addAttribute("page",1);
        model.addAttribute("nowFilterType",FilterType.all);
        model.addAttribute("filterTypes",FilterType.values());
        model.addAttribute("columnTypes", ColumnType.values());
        model.addAttribute("technologyTypes",TechnologyType.values());
        model.addAttribute("topJieList",jieServsice.getTopList());
        model.addAttribute("jieList",jieList);
        model.addAttribute("pageInfo",new PageInfo<Jie>(jieList,5));
        return "index";
    }

    @GetMapping("/q/jie/{id}")
    public String jieDetail(@PathVariable Integer id,Model model){
        return jieDetail(id,1,model);
    }

    @GetMapping("/q/jie/{id}/page/{page}")
    public String jieDetail(@PathVariable Integer id,@PathVariable Integer page,Model model){
        if(StringUtil.isEmpty(page)){
            page=1;
        }
        log.info("分页帖子主键{}",id);
        Jie jie = jieServsice.getJieDetailById(id);
        Page<Reply> repliePage = replyService.getReplyByJieId(id, page, 10);
        //
        String jieContent = jie.getJieContent();
        if(jieContent.length() > 120){
            jieContent=jieContent.substring(0,120);
        }
        model.addAttribute("jie",jie);
        model.addAttribute("title",jie.getJieTitle());
        model.addAttribute("content",jieContent);
        model.addAttribute("replyList",repliePage);
        model.addAttribute("pageInfo",new PageInfo<Reply>(repliePage,5));
        model.addAttribute("nowTechnologyType",jie.getTechnologyType());
        model.addAttribute("nowColumnType",jie.getColumnType());
        model.addAttribute("columnTypes", ColumnType.values());
        model.addAttribute("technologyTypes",TechnologyType.values());
        return "jie/detail";
    }


    /**
     * 帖子类型单条件过滤
     * @param columnType
     * @return
     */
    @GetMapping("/q/{columnType}")
    public String jieIndex(@PathVariable ColumnType columnType,Model model){
        log.info("帖子类型{}",columnType);
        Page<Jie> jieList = jieServsice.getJieList(new Jie()
                        .setColumnType(columnType == ColumnType.all ? null : columnType == ColumnType.all ? null : columnType)
                , null, 1, 20);
        model.addAttribute("jieList",jieList);
        model.addAttribute("pageInfo",new PageInfo<Jie>(jieList,5));
        model.addAttribute("nowFilterType",FilterType.all);
        model.addAttribute("nowColumnType",columnType);
        model.addAttribute("filterTypes",FilterType.values());
        model.addAttribute("technologyTypes",TechnologyType.values());
        model.addAttribute("columnTypes", ColumnType.values());
        return "jie/index";
    }

    /**
     * 帖子类型单条件过滤
     * @param columnType
     * @return
     */
    @GetMapping("/q/{columnType}/page/{page}")
    public String jieIndex(@PathVariable ColumnType columnType,@PathVariable Integer page,Model model){
        log.info("帖子类型{}",columnType);
        log.info("页数{}",page);
        Page<Jie> jieList = jieServsice.getJieList(new Jie()
                        .setColumnType(columnType == ColumnType.all ? null : columnType == ColumnType.all ? null : columnType)
                , null, page, 20);
        model.addAttribute("jieList",jieList);
        model.addAttribute("pageInfo",new PageInfo<Jie>(jieList,5));
        model.addAttribute("nowFilterType",FilterType.all);
        model.addAttribute("technologyTypes",TechnologyType.values());
        model.addAttribute("columnTypes", ColumnType.values());
        model.addAttribute("filterTypes",FilterType.values());
        model.addAttribute("nowColumnType",columnType);
        return "jie/index";
    }

    /**
     * 帖子类型多条件过滤
     * @param columnType 帖子类型
     * @param technologyType 技术类型
     * @return
     */
    @GetMapping("/q/{columnType}/{technologyType}")
    public String jieIndex(@PathVariable ColumnType columnType
            , @PathVariable TechnologyType technologyType,Model model){
        log.info("帖子类型{}",columnType);
        log.info("技术类型{}",technologyType);

        Page<Jie> jieList = jieServsice.getJieList(new Jie()
                        .setColumnType(columnType == ColumnType.all ? null : columnType)
                        .setTechnologyType(technologyType == TechnologyType.all ? null : technologyType)
                , null, 1, 20);
        model.addAttribute("jieList",jieList);
        model.addAttribute("pageInfo",new PageInfo<Jie>(jieList,5));
        model.addAttribute("page",1);
        model.addAttribute("nowFilterType",FilterType.all);
        model.addAttribute("columnTypes", ColumnType.values());
        model.addAttribute("filterTypes",FilterType.values());
        model.addAttribute("nowColumnType",columnType);
        model.addAttribute("nowTechnologyType",technologyType);
        model.addAttribute("technologyTypes",TechnologyType.values());
        return "jie/index";
    }

    /**
     * 帖子类型多条件过滤
     * @param columnType 帖子类型
     * @param technologyType 技术类型
     * @return
     */
    @GetMapping("/q/{columnType}/{technologyType}/page/{page}")
    public String jieIndex(@PathVariable ColumnType columnType
            , TechnologyType technologyType,@PathVariable Integer page,Model model){
        log.info("帖子类型{}",columnType);
        log.info("技术类型{}",technologyType);
        log.info("页数{}",page);
        Page<Jie> jieList = jieServsice.getJieList(new Jie()
                        .setColumnType(columnType == ColumnType.all ? null : columnType)
                        .setTechnologyType(technologyType == TechnologyType.all ? null : technologyType)
                , null, page, 20);

        model.addAttribute("jieList",jieList);
        model.addAttribute("pageInfo",new PageInfo<Jie>(jieList,5));
        model.addAttribute("nowFilterType",FilterType.all);
        model.addAttribute("columnTypes", ColumnType.values());
        model.addAttribute("filterTypes",FilterType.values());
        model.addAttribute("nowColumnType",columnType);
        model.addAttribute("nowTechnologyType",technologyType);
        model.addAttribute("technologyTypes",TechnologyType.values());
        return "jie/index";
    }

    /**
     * 主页条件查询
     * @param columnType 帖子类型过滤 ：提问、分享等
     * @param technologyType 技术类型过滤： java、web等
     * @param filterType 帖子状态过滤: 已结、未结等
     * @return
     */
    @GetMapping("/q/{columnType}/{technologyType}/{filterType}")
    public String jieIndex(@PathVariable ColumnType columnType
            , @PathVariable TechnologyType technologyType
            ,@PathVariable FilterType filterType,Model model){
        log.info("帖子类型{}",columnType);
        log.info("技术类型{}",technologyType);
        log.info("帖子状态{}",filterType);

        Page<Jie> jieList = jieServsice.getJieList(new Jie()
                        .setColumnType(columnType == ColumnType.all ? null : columnType)
                        .setTechnologyType(technologyType == TechnologyType.all ? null : technologyType)
                , filterType, 1, 20);
        model.addAttribute("jieList",jieList);
        model.addAttribute("pageInfo",new PageInfo<Jie>(jieList,5));
        model.addAttribute("nowFilterType",filterType);
        model.addAttribute("nowColumnType",columnType);
        model.addAttribute("nowTechnologyType",technologyType);
        model.addAttribute("columnTypes", ColumnType.values());
        model.addAttribute("filterTypes",FilterType.values());
        model.addAttribute("technologyTypes",TechnologyType.values());
        return "jie/index";
    }

    /**
     * 主页条件查询
     * @param columnType 帖子类型过滤 ：提问、分享等
     * @param technologyType 技术类型过滤： java、web等
     * @param filterType 帖子状态过滤: 已结、未结等
     * @return
     */
    @GetMapping("/q/{columnType}/{technologyType}/{filterType}/page/{page}")
    public String jieIndex(@PathVariable ColumnType columnType
            , @PathVariable TechnologyType technologyType
            ,@PathVariable FilterType filterType
            ,@PathVariable Integer page,Model model){
        log.info("帖子类型{}",columnType);
        log.info("技术类型{}",technologyType);
        log.info("帖子状态{}",filterType);
        log.info("分页条件{}",page);
        Page<Jie> jieList = jieServsice.getJieList(new Jie()
                        .setColumnType(columnType == ColumnType.all ? null : columnType)
                        .setTechnologyType(technologyType == TechnologyType.all ? null : technologyType)
                , filterType, page, 20);
        model.addAttribute("jieList",jieList);
        model.addAttribute("pageInfo",new PageInfo<Jie>(jieList,5));
        model.addAttribute("nowFilterType",filterType);
        model.addAttribute("columnTypes", ColumnType.values());
        model.addAttribute("nowColumnType",columnType);
        model.addAttribute("filterTypes",FilterType.values());
        model.addAttribute("nowTechnologyType",technologyType);
        model.addAttribute("technologyTypes",TechnologyType.values());
        return "jie/index";
    }


}
